package view;



import dao.UserDao;
import model.User;
import util.DbUtil;
import util.toolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Random;

public class RegFrm {




    private JFrame jf;

    private DbUtil dbUtil;


    private UserDao userDao;


    JTextField nameField;
    JTextField passwordField;
    JTextField phoneField;
    JComboBox<String> genderComboBox;


    public RegFrm(){

        dbUtil=new DbUtil();

        jf=new JFrame("用户注册");

        jf.setVisible(true);

        jf.getContentPane().setFont(new Font("幼圆",Font.BOLD,14));

        jf.setBounds(700,250,500,467);

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jf.getContentPane().setLayout(null);





        JLabel title =new JLabel("用户注册");
        title.setForeground(Color.BLACK);
        title.setFont(new Font("幼圆",Font.BOLD,16));
        title.setBounds(230, 28, 65, 40);
        jf.getContentPane().add(title);



        JLabel nameLabel = new JLabel("用户名:");
        nameLabel.setForeground(Color.BLACK);
        nameLabel.setFont(new Font("幼圆",Font.BOLD,16));
        nameLabel.setBounds(120, 78, 65, 40);
        jf.getContentPane().add(nameLabel);

         nameField = new JTextField();
        nameField.setFont(new Font("Dialog",Font.BOLD,14));
        nameField.setColumns(10);
        nameField.setBounds(198, 78, 164, 30);
        jf.getContentPane().add(nameField);





        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setForeground(Color.BLACK);
        passwordLabel.setFont(new Font("幼圆",Font.BOLD,16));
        passwordLabel.setBounds(120, 128, 65, 40);
        jf.getContentPane().add(passwordLabel);


         passwordField =new JTextField();
        passwordField.setFont(new Font("Dialog",Font.BOLD,14));
        passwordField.setToolTipText("");
        passwordField.setColumns(10);
        passwordField.setBounds(198, 128, 164, 30);
        jf.getContentPane().add(passwordField);





        JLabel phoneLabel = new JLabel("手机号:");
        phoneLabel.setForeground(Color.BLACK);
        phoneLabel.setFont(new Font("幼圆",Font.BOLD,16));
        phoneLabel.setBounds(120, 178, 65, 40);
        jf.getContentPane().add(phoneLabel);

         phoneField = new JTextField();
        phoneField.setFont(new Font("Dialog",Font.BOLD,14));
        phoneField.setColumns(10);
        phoneField.setBounds(198, 178, 164, 30);
        jf.getContentPane().add(phoneField);






        JLabel genderLabel = new JLabel("性别:");
        genderLabel.setForeground(Color.BLACK);
        genderLabel.setFont(new Font("幼圆",Font.BOLD,16));
        genderLabel.setBounds(120, 228, 65, 40);
        jf.getContentPane().add(genderLabel);

        String[] genderOptions = {"男", "女"};
         genderComboBox = new JComboBox<>(genderOptions);
        genderComboBox.setBounds(198, 228, 100, 30);
        jf.getContentPane().add(genderComboBox);





        JLabel codeLabel = new JLabel("验证码:");
        codeLabel.setForeground(Color.BLACK);
        codeLabel.setFont(new Font("幼圆",Font.BOLD,16));
        codeLabel.setBounds(120, 284, 65, 40);
        jf.getContentPane().add(codeLabel);

        JTextField codeField = new JTextField();
        codeField.setFont(new Font("Dialog",Font.BOLD,14));
        codeField.setColumns(10);
        codeField.setBounds(198, 284, 100, 30);
        jf.getContentPane().add(codeField);


        String url="J:\\ProjectFile\\myBookSystem\\src\\main\\resources\\";
        String png= "tupian";
        Random r=new Random();
        int i = r.nextInt(3);
        String filename =url+png+i+".png";
        System.out.println(filename);

        ImageIcon codeIcon = new ImageIcon(filename);
        Image scaledImage = codeIcon.getImage().getScaledInstance(70, 30, Image.SCALE_SMOOTH);

        ImageIcon scaledCodeIcon = new ImageIcon(scaledImage);
        JLabel codeIconLabel = new JLabel(scaledCodeIcon);
        codeIconLabel.setBounds(300, 284, scaledCodeIcon.getIconWidth(), scaledCodeIcon.getIconHeight());
        jf.getContentPane().add(codeIconLabel);









        JButton registerButton = new JButton("注册");
        registerButton.setBounds(150, 320, 100, 30);
        jf.getContentPane().add(registerButton);




        JButton loginButton = new JButton("前往登录页面");
        loginButton.setBounds(270, 320, 150, 30);
        jf.getContentPane().add(loginButton);


















        passwordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {


            }

            @Override
            public void focusLost(FocusEvent e) {

                String pwd = passwordField.getText();


                if (toolUtil.isEmpty(pwd)){

                    passwordField.setText("密码不能为空");
                    passwordField.setForeground (Color.RED);


                }
                else {
                    // boolean flag =
                    //         pwd.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\\\d)[a-zA-Z\\\\d]{6,16}$");
                    // if (flag) {
                    //     passwordField.setText("√");
                    //     passwordField.setForeground(Color.GREEN);
                    // } else {
                    //     JOptionPane.showMessageDialog(null,"密码需为6~16位数字和字母的组合");
                    //     passwordField.setText("");
                    // }
                }

            }
        });






        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                RegCheck(e);


            }
        });




        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                jf.dispose();

                new LoginFrm();

            }
        });













    }





    private void RegCheck(ActionEvent e) {


        String username = nameField.getText();
        String password = passwordField.getText();
        String phone = phoneField.getText();

        Object selectedItem = genderComboBox.getSelectedItem();
        String sex = (String) selectedItem;

        User user =new User();
        user.setUserName(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setSex(sex);
        user.setRole(1);
        Connection con=null;

        userDao=new UserDao();


        try {

            con=dbUtil.getConnection();
            int i=  userDao.addUser(con,user);

            if(i==2){

                JOptionPane.showMessageDialog(null,"该用户名已存在,请重新注册");

            }else if(i ==0){
                JOptionPane.showMessageDialog(null,"注册失败");

            }else{
                JOptionPane.showMessageDialog(null,"注册成功");
                jf.dispose();
                new LoginFrm();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {

            try {
                dbUtil.closeCon(con);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }


        }


    }












}
